﻿using Clirelmgt.Manager;
using Clirelmgt.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Clirelmgt.Controllers
{
    public class ModuleController : Controller
    {
        public ActionResult Index()
        {
            List<ModuleModel> model = new List<ModuleModel>();
            using (CRMDataContext dataContext = new CRMDataContext())
            {
                var result = dataContext.GetModules();
                foreach (var item in result)
                {
                    model.Add(new ModuleModel()
                    {
                        ModuleID = item.ModuleID,
                        Name = item.Name,
                        Description = item.Description,
                        MenuName = item.MenuName,
                        IconImage = item.IconImage,
                        IsStandard = item.IsStandard
                    });
                }
            }

            return View(model);
        }

        public ActionResult Card(short ID)
        {
            ModuleModel model = new ModuleModel();
            using (CRMDataContext dataContext = new CRMDataContext())
            {
                var result = dataContext.GetModuleInfo(ID);
                foreach (var item in result)
                {
                    model.Name = item.Name;
                    model.Description = item.Description;
                    model.MenuName = item.MenuName;
                    model.IconImage = item.IconImage;
                    model.IsStandard = item.IsStandard;
                }
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult Card(short ID, ModuleModel model)
        {
            using (CRMDataContext dataContext = new CRMDataContext())
            {
                dataContext.UpdateModuleInfo(ID, model.Description, model.MenuName, model.IconImage);
            }

            return RedirectToAction("Index");
        }

        public ActionResult Objects()
        {
            string queryType = "";
            short moduleId = 0;
            string type = "";
            if (Request.QueryString.AllKeys.Contains("module"))
            {
                queryType = "module";
                moduleId = Convert.ToInt16(Request.QueryString["module"]);
            }
            else if (Request.QueryString.AllKeys.Contains("type"))
            {
                queryType = "type";
                type = Request.QueryString["type"];
            }

            List<ObjectModel> model = new List<ObjectModel>();
            using (CRMDataContext dataContext = new CRMDataContext())
            {
                if (queryType == "module")
                {
                    var result = dataContext.GetAppObjectsByModule(moduleId);
                    foreach (var item in result)
                    {
                        model.Add(new ObjectModel()
                        {
                            ModuleID = Convert.ToInt16(item.ModuleID),
                            Name = item.Name,
                            LinkTable = item.LinkTable,
                            ModuleName = item.ModuleName,
                            ObjectType = item.ObjectType
                        });
                    }
                }
                else if (queryType == "type")
                {
                    var result = dataContext.GetAppObjectByType(type.ToUpperInvariant());
                    foreach (var item in result)
                    {
                        model.Add(new ObjectModel()
                        {
                            ModuleID = Convert.ToInt16(item.ModuleID),
                            Name = item.Name,
                            LinkTable = item.LinkTable,
                            ModuleName = item.ModuleName,
                            ObjectType = item.ObjectType
                        });
                    }
                }
                else
                {
                    var result = dataContext.GetAllAppObjects();
                    foreach (var item in result)
                    {
                        model.Add(new ObjectModel()
                        {
                            ModuleID = Convert.ToInt16(item.ModuleID),
                            Name = item.Name,
                            LinkTable = item.LinkTable,
                            ModuleName = item.ModuleName,
                            ObjectType = item.ObjectType
                        });
                    }
                }
            }

            return View(model);
        }
    }
}
